var app = getApp();
Page({

  data: {
    status:'点击选择班次',    
    status2:'点击选择结算方式',
    itemList: ['白班', '夜班'],
    itemList2: ['按天', '计时','计件'],
    pageInfo: '',
    index: 0,   
    index2: 0,
    zzz:'2',
    array: ['早班', '晚班'],
    array2: ['上班', '下班'],
    date: '',
    banci:'',
    jiesuanfangshi:'',
    liyou:'',
    date2:'',
    array3: [],
    index3:0,
    array4: [],
    index4:0
  },

  onLoad: function () {
    var that = this;
    if (!wx.getStorageSync('openid')) {
      app.error("请登录后再操作");
      return false;
    }
    var date = new Date();

    var mingtian = date.getDate()+1

    var houtian = date.getDate()+2

    var todate = date.getFullYear() + "-" + ((date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : date.getMonth()+1) + "-" + (mingtian < 10 ? ("0" + mingtian) : mingtian);
    
    var todate2 = date.getFullYear() + "-" + ((date.getMonth() + 1) < 10 ? ("0" + (date.getMonth() + 1)) : date.getMonth()+1) + "-" + (houtian < 10 ? ("0" + houtian) : houtian);

    that.setData({ start: todate}); 
    that.setData({ date: todate});
    that.setData({ date2: todate2});
    
    var hours = ['0:00']
    for (var i=0;i<23;i++)
    { 
      var xx = (i+1) +":00"
     hours.push(xx)
    }

    that.setData({ array3: hours});
    that.setData({ array4: hours});
    
  },
  changezt(){
    var that = this
    wx.showActionSheet({
      itemList: ['白班', '夜班'],
      success(res) {
        that.setData({ status: that.data.itemList[res.tapIndex]});
        that.setData({ choose: res.tapIndex});
     
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })
  },
  changezt2(){
    var that = this
    wx.showActionSheet({
      itemList: ['按天', '计时','计件'],
      success(res) {
        that.setData({ status2: that.data.itemList2[res.tapIndex]});
        that.setData({ choose2: res.tapIndex});
     
      },
      fail(res) {
        console.log(res.errMsg)
      }
    })
  },
  
  bindPickerChange: function(e) {
  
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index3: e.detail.value
    })

  },

  bindPickerChange2: function(e) {
  
    // console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index4: e.detail.value
    })


  },

 

  chaxun(){
    var kaishi = this.data.date
    wx.request({
      url: app.apiUrl + '/api/index/bukachaxun',
      data: {
        kaishi:kaishi,
        openid:wx.getStorageSync('openid'),
      },
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: res=> {
        if(res.data == 1){
          wx.showModal({
            title: '温馨提示',
            content: '您在该日期已有打卡记录,请前往考勤记录页面修改',
            showCancel: false
          });
        }else{
          wx.showModal({
            title: '温馨提示',
            content: '您符合该日期的补卡条件,请输入正确的数据提交申请',
            showCancel: false
          });
          this.setData({ zzz: 1});
        }
      
     
      }
    })
  },
  tijiao(){
     
   
      if(this.data.status == "点击选择班次"){
        wx.showModal({
          title: '温馨提示',
          content: '请选择班次',
          showCancel: false
        });
        return false
      }
      console.log(this.data.choose);


      if(this.data.status2 == "点击选择结算方式"){
        wx.showModal({
          title: '温馨提示',
          content: '请选择结算方式',
          showCancel: false
        });
        return false
      }
      console.log(this.data.choose2);

      var liyou = this.data.liyou
      if(!liyou){
        wx.showModal({
          title: '温馨提示',
          content: '请输入补卡理由',
          showCancel: false
        });
        return false
      }

    wx.request({
      url: app.apiUrl + '/api/index/bukasq',
      data: {
        riqi:this.data.date,
        kaishi:this.data.array3[this.data.index3],
        jieshu:this.data.array4[this.data.index4],
        liyou:liyou,
        banci:this.data.choose,
        ishours:this.data.choose2,
        openid:wx.getStorageSync('openid'),
      },
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      success: res=> {
        if(res.data==1){
          wx.showModal({
            title: '温馨提示',
            content:'申请成功,等待审核',
            showCancel: false
          });

          setTimeout(function () {
            wx.navigateTo({
              url: '/page/my/index',
            });
          }, 2000)
        }else{
          wx.showModal({
            title: '温馨提示',
            content: res.data,
            showCancel: false
          });
        }
 
   
      }
    })

  },
  bindKeyInput(e){
      console.log(e.detail.value)
      this.setData({ liyou: e.detail.value});
  },

  bindDateChange: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },

  bindDateChange2: function(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date2: e.detail.value
    })
  },



})